using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

namespace Dbam.DbFramework.Tests
{
	[TestFixture]
	public class ObjectRelationalMapperTests
	{
		private IDbFactory _dbFactory;

		[SetUp]
		public void SetupTest()
		{
			if(_dbFactory != null)
			{
				_dbFactory.Dispose();
				_dbFactory = null;
			}
			_dbFactory = new SqlDbFactory(TestSettings.Default.DbSampleConnectionString);
		}

		[Test]
		public void ScenarioCaseOne()
		{
			ObjectRelationalMapper orm = new ObjectRelationalMapper(_dbFactory);
			
			Account account = new Account();
			account.Id = Guid.NewGuid();
			account.Name = "Test Account";
			account.OpenDate = DateTime.Parse(@"3/3/3333");
			for(int index = 0; index < 5; index++)
			{
				Session session = new Session();
				session.Id = Guid.NewGuid();
				session.ParentAccountId = account.Id;
				session.BeginTime = DateTime.Parse("1/2/3456");
				session.EndTime = DateTime.Parse("6/5/4321");
				account.Sessions.Add(session);
			}
			orm.InsertEntity(account);
			Assert.IsTrue(orm.EntityExists(account));
			for(int index = 0; index < account.Sessions.Count; index++)
				Assert.IsTrue(orm.EntityExists(account.Sessions[index]));

			account.Name = "Test Account (Updated)";
			account.LastLogon = DateTime.Now;
			account.Sessions[0].EndTime = DateTime.Parse("8/8/8888");
			orm.UpdateEntity(account);
			Assert.IsTrue(orm.EntityExists(account));

			orm.SelectEntity(account);
			Assert.AreEqual("Test Account (Updated)", account.Name);
			Assert.AreEqual(new DateTime(8888, 8, 8), account.Sessions[0].EndTime);

			orm.DeleteEntity(account);
			Assert.IsFalse(orm.EntityExists(account));
		}
	}
}